import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Navbar from '../components/Layout/Navbar';
import SocialPage from "../components/SocialPage/ SocialPage";
import LoginPage from '../components/LoginPage/LoginPage';
import RegisterPage from '../components/RegisterPage/RegisterPage';
import ProfilePage from '../components/ProfilePage/ProfilePage';
import WorksPage from '../components/WorksPage/WorksPage';
import UserPage from '../components/UserPage/UserPage';
import AdminPostManagement from '../components/AdminPostManagement/AdminPostManagement';

function AppRouter({ user, token, onLogin, onLogout, setUser }) {
    // 检查用户是否为管理员
    const isAdmin = user?.data.isAdmin;

    return (
        <Router>
            <div className="app">
                <Navbar
                    user={user}
                    onLogout={onLogout}
                    isAdmin={isAdmin}
                />

                <main className="page-container">
                    <Routes>
                        {/* 未登录用户路由 */}
                        {!user ? (
                            <>
                                <Route
                                    path="/login"
                                    element={
                                        <LoginPage
                                            onLogin={onLogin}
                                        />
                                    }
                                />
                                <Route
                                    path="/register"
                                    element={<RegisterPage />}
                                />
                                {/* 【新增】未登录用户访问管理员页面时重定向到登录页 */}
                                <Route
                                    path="/admin/*"
                                    element={<Navigate to="/login" replace />}
                                />
                                {/* 未登录用户访问受保护路由时重定向到登录页 */}
                                <Route
                                    path="/social"
                                    element={<Navigate to="/login" replace />}
                                />
                                <Route
                                    path="/profile"
                                    element={<Navigate to="/login" replace />}
                                />
                                <Route
                                    path="/works"
                                    element={<Navigate to="/login" replace />}
                                />
                                <Route
                                    path="/users"
                                    element={<Navigate to="/login" replace />}
                                />
                                {/* 默认路由：未登录时重定向到登录页 */}
                                <Route
                                    path="/"
                                    element={<Navigate to="/login" replace />}
                                />
                                {/* 404路由：未登录时重定向到登录页 */}
                                <Route
                                    path="*"
                                    element={<Navigate to="/login" replace />}
                                />
                            </>
                        ) : (
                            /* 已登录用户路由 */
                            <>
                                <Route
                                    path="/social"
                                    element={
                                        <SocialPage
                                            user={user}
                                            token={token}
                                        />
                                    }
                                />
                                {/* 【新增】管理员路由 */}
                                <Route
                                    path="/admin/posts"
                                    element={
                                        isAdmin ? (
                                            <AdminPostManagement
                                                user={user}
                                                token={token}
                                            />
                                        ) : (
                                            <Navigate to="/social" replace />
                                        )
                                    }
                                />

                                {/* 【新增】管理员根路由重定向 */}
                                <Route
                                    path="/admin"
                                    element={
                                        isAdmin ? (
                                            <Navigate to="/admin/posts" replace />
                                        ) : (
                                            <Navigate to="/social" replace />
                                        )
                                    }
                                />
                                <Route
                                    path="/profile"
                                    element={
                                        <ProfilePage
                                            user={user}
                                            token={token}
                                            setUser={setUser}
                                        />
                                    }
                                />
                                <Route
                                    path="/works"
                                    element={
                                        <WorksPage
                                            user={user}
                                            token={token}
                                        />
                                    }
                                />
                                {/* 管理员路由 */}
                                {isAdmin && (
                                    <Route
                                        path="/users"
                                        element={
                                            <UserPage
                                                user={user}
                                                token={token}
                                            />
                                        }
                                    />
                                )}
                                {/* 非管理员访问用户管理页面时重定向到社交模块 */}
                                {!isAdmin && (
                                    <Route
                                        path="/users"
                                        element={<Navigate to="/social" replace />}
                                    />
                                )}
                                {/* 已登录用户访问登录/注册页时重定向到社交模块 */}
                                <Route
                                    path="/login"
                                    element={<Navigate to="/social" replace />}
                                />
                                <Route
                                    path="/register"
                                    element={<Navigate to="/social" replace />}
                                />
                                {/* 默认路由：已登录时重定向到社交模块 */}
                                <Route
                                    path="/"
                                    element={<Navigate to="/social" replace />}
                                />
                                {/* 404路由：已登录时重定向到社交模块 */}
                                <Route
                                    path="*"
                                    element={<Navigate to="/social" replace />}
                                />
                            </>
                        )}
                    </Routes>
                </main>
            </div>
        </Router>
    );
}

export default AppRouter;